package com.blog.boot.webapp.controller;

import com.blog.boot.api.admin.vo.FileModel;
import com.blog.boot.api.common.RestMsg;
import com.blog.boot.webapp.controller.common.BaseController;
import com.blog.boot.webapp.utils.GetFileList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class FileController extends BaseController {

    public static String CLASSPATH = java.net.URLDecoder.decode(System.getProperty("user.dir")).replace("\\","/");

    @RequestMapping("/file")
    public RestMsg.RestModelAndView index(HttpServletRequest request,String path) {
        String s = StringUtils.isEmpty(path) ? "C:/java/webstatic/resources" : path;
        File file = new File(s);
        int i = s.lastIndexOf(".");
        String fileName = "";
        String fileContent= "";
        if(i!=-1){
            fileName = s.substring(i+1, s.length());
            System.out.println(fileName);
            if( "png".equals( fileName ) || "jpg".equals( fileName ) || "gif".equals( fileName )){
                request.setAttribute("err","很抱歉，暂时不支持查看"+fileName+"格式");
                return restFactory.setModelAndView("/file.html");
            }
            fileContent = GetFileList.getContentTextByFile(file);
            request.setAttribute("centent",StringUtils.isEmpty(fileContent)?"":fileContent);
        }else{
            /*Map<String, Object> fileListByFile = null;
            try {
                fileListByFile = GetFileList.getFileListByFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            JSONArray dir =(JSONArray) fileListByFile.get("dir");
            JSONArray _file =(JSONArray) fileListByFile.get("file");
            request.setAttribute("dir",dir);
            request.setAttribute("file",_file);*/
        }
        request.setAttribute("current_path",s);
        return restFactory.setModelAndView("/file.html");
    }

    @RequestMapping("/file/list")
    @ResponseBody
    public RestMsg getFileList(String path){//生产环境一定记得要修改路径
        if(StringUtils.isEmpty(path)) path = "/";
        RestMsg msg = restFactory.getMsg();
        File file = new File(CLASSPATH+path);
        if(!file.isDirectory()) {
            String fileName = "";
            String fileContent= "";
            int i = path.lastIndexOf(".");
            fileName = path.substring(i+1, path.length());
            if( "png".equals( fileName ) || "jpg".equals( fileName ) || "gif".equals( fileName )){
                msg.error("很抱歉，暂时不支持查看"+fileName+"格式").setData(null);
                return msg;
            }
            fileContent = GetFileList.getContentTextByFile(file);
            if("html".equals(fileName)){
                fileContent = fileContent.replaceAll("<","&lt;").replaceAll(">","&gt;");
            }
            msg.put("content",StringUtils.isEmpty(fileContent)?"":fileContent);
            msg.put("type",fileName);
            List<Object> objects = new ArrayList<>();
            objects.add(GetFileList.getFileByObj(file));
            msg.setData(objects);
        }else{
            JSONArray fileList = null;
            try {
                fileList = GetFileList.getFileListByFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            msg.setData(fileList);
        }
        msg.put("current_path",path);
        return msg;
    }

    @RequestMapping("/file/up")
    @ResponseBody
    public RestMsg upFile(FileModel fileModel){
        RestMsg msg = restFactory.getMsg();
        File file = new File(CLASSPATH+fileModel.getFilePath());
        GetFileList.upfile(file,fileModel);
        return msg;
    }

    @RequestMapping("/file/del")
    @ResponseBody
    public RestMsg delFile(String fileListStr,String current_path){
        RestMsg msg = restFactory.getMsg();
        JSONArray jsonArray1 = JSONArray.fromObject(fileListStr);
        for (Object o :jsonArray1) {
            JSONObject o1 = (JSONObject) o;
            FileModel o2 = (FileModel)JSONObject.toBean(o1, FileModel.class);
            o2.setFilePath(CLASSPATH+o2.getFilePath());
            GetFileList.delFile(o2);
        }
        msg.put("url",current_path);
        return msg;
    }

    @RequestMapping("/file/add")
    @ResponseBody
    public RestMsg addFile(String fileName,String current_path,String fileType){
        RestMsg msg = restFactory.getMsg();
        JSONObject jsonObject = GetFileList.createFile(fileName, CLASSPATH+current_path,fileType);
        if(org.apache.commons.lang.StringUtils.isNotEmpty((String) jsonObject.get("code"))){
            return msg.setCode((String) jsonObject.get("code")).setMsg((String) jsonObject.get("msg"));
        }
        return msg.error((String) jsonObject.get("msg"));
    }
}
